package me.shurufa.activities;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.Bind;
import de.greenrobot.event.EventBus;
import me.shurufa.R;
import me.shurufa.utils.Constants;
import me.shurufa.utils.Global;
import me.shurufa.utils.Settings;
import me.shurufa.widget.colordialog.ColorDialog;

/**
 * 从我的ProfileFragment 点击设置弹出的页面
 *
 * @FileName: me.shurufa.activities.SettingsActivity.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-01-19 10:30
 */
public class SettingsActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.left_text)
    TextView leftText;
    @Bind(R.id.right_image)
    ImageView rightImage;
    @Bind(R.id.right_text)
    TextView rightText;
    @Bind(R.id.user_about_us_layout)
    LinearLayout userAboutUsLayout;
    @Bind(R.id.user_feedback_layout)
    LinearLayout userFeedbackLayout;
    @Bind(R.id.back_login)
    TextView backLogin;

    private Handler mHandler = new Handler();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        activity_layout_res = R.layout.activity_settings;
        super.onCreate(savedInstanceState);
        initUI();
    }

    private void initUI() {
        titleTextView.setText(getString(R.string.setting));
        userAboutUsLayout.setOnClickListener(this);
        userFeedbackLayout.setOnClickListener(this);
        backLogin.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()){
            case R.id.user_about_us_layout:
                intent = new Intent(SettingsActivity.this,AboutUsActivity.class);
                startActivity(intent);
                break;
            case R.id.user_feedback_layout:
                intent = new Intent(SettingsActivity.this,FeedBackActivity.class);
                startActivity(intent);
                break;
            case R.id.back_login:
                showDlgVerifyLogout();
                break;
        }
    }

    /**
     * 对话框 确认是否退出登录
     */
    private void showDlgVerifyLogout() {
        ColorDialog dialog = new ColorDialog(this);
        dialog.hideTitleLayout();
        dialog.setContentText(getString(R.string.info_back_logout));
        dialog.setColor(getResources().getColor(R.color.gray7));
        dialog.setContentTextColor(getResources().getColor(R.color.gray2));
        dialog.setPositiveListener(getString(R.string.ok), new ColorDialog.OnPositiveListener() {
            @Override
            public void onClick(ColorDialog dialog) {
                dialog.dismiss();

                // 延迟150毫秒退出
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        logout();
                    }
                }, 150);

            }
        })
        .setNegativeListener(getString(R.string.cancel), new ColorDialog.OnNegativeListener() {
            @Override
            public void onClick(ColorDialog dialog) {
                dialog.dismiss();
            }
        }).show();
    }

    /**
     * 退出登录
     */
    private void logout() {
        EventBus.getDefault().post(new Intent(Constants.ACTION_USER_CHANGE));
        Settings.delUser(String.valueOf(Global.currentUser.uid));
        Global.currentUser = null;
        Intent intent = new Intent(SettingsActivity.this, NewLoginActivity.class);
        startActivity(intent);
        finish();
    }
}
